package top.xia17.plugins.fast.dynamic.datasource.dao;

import top.xia17.plugins.fast.common.database.dynamic.DatasourceInfo;
import top.xia17.plugins.fast.common.database.dynamic.DatasourceLoader;
import top.xia17.plugins.fast.common.exceptions.BadConfigException;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;
import lombok.RequiredArgsConstructor;

/**
 * jpa数据源信息加载
 *
 * @author xia17
 * @date 2022/4/13
 */
@RequiredArgsConstructor
public class JpaDatasourceLoader implements DatasourceLoader {

    private final DatasourceRepository datasourceRepository;

    @Override
    public DatasourceInfo load(String name) {
        DatasourceEntity datasourceEntity = datasourceRepository.findByEngName(name).orElseThrow(() -> new BadRequestException("未找到数据源" + name));
        if (!datasourceEntity.getEnabled()) {
            throw new BadConfigException("数据源" + name + "已被禁止使用！");
        }
        return datasourceEntity.buildDatasourceInfo(null);
    }


}
